﻿//动作参数关联组
packageContent.actionParamRelaGrp = function() {
	//局部全局变量
	var tempPack1;
	var actionItemMap;
	
	var _initParamRelaPageInfo = function() {
		//校验前做包对象转换
		tempPack1 = $.extend(true,{},pack);
		tempPack1 = relocatePackForBizProcess(tempPack1);
		
		//不存在直接返回
		var paramActionRelaArray  = tempPack1.paramActionRela;
		if (paramActionRelaArray.length == 0){
			return ;
		}
		
		//转换数据所需的条目与参数
		actionItemMap = pack.getActionItemMap();
		var actionParameterMap = tempPack1.actionParamterMap;
		
		var checkBoxFlag = "";
		var paramTable = $("#tb_p_paramRelaGrp");
		for (var i =0; i< paramActionRelaArray.length; i++) {
			
			var paramActionRela = paramActionRelaArray[i];
			var parameterId = paramActionRela.getParameterId();
			var itemId = paramActionRela.getActionItemId();
			var value = paramActionRela.getValue();
			var parameterType = paramActionRela.parameterType;
			var grpId = paramActionRela.getGrpId();
			
			//确定是否展示
			var rowSpan = getRowspan(tempPack1,itemId);
			if( rowSpan < 2) {
				continue ;
			}
			
			//动态添加数据
			var tr = $("<tr>");
			
			//参数名称
			var _parentActionItemId = paramActionRela.parentActionItemId;
			var itemParentNumber  = _getItemNumber(_parentActionItemId);
			var itemParent = f_getActionItem(_parentActionItemId);
			var paramName = '【'+itemParentNumber+'】'+ actionParameterMap.get(parameterId).getName();
			var tdParamName  = $("<td>");
			tdParamName.append(paramName);
			tdParamName.attr('title',itemParent.getDescription());

			//参数值
			var tdParamValue ;
			//产品新装已有类型，可选Y|N|D
			if (parameterType == params.S01.type) {
				tdParamValue = $("<td>").append(params.S01[value])
			} else if(parameterType == params.S03.type){//设置预后标识
				tdParamValue = $("<td>").append(params.S03[value])
			} else {
				tdParamValue = $("<td>").append(value)
			}
			
			//被决定的 动作条目
			var itemDesc;
			//包修改时的转换
			var actionItem = f_getActionItem(itemId);
			var itemNumber  = _getItemNumber(itemId);
			if (actionItem.getDescription()) {
				itemDesc = '【'+itemNumber+'】 '+actionItem.getDescription();
			} else {
				itemDesc = '【'+itemNumber+'】';
			}
			var tdActionItem  = $("<td>").append(itemDesc)
			
			//两个参数决定一个条目时，不给与选择功能
			var inputCheck = $('<input>');
			inputCheck.attr('paramId',parameterId);
			inputCheck.attr('itemId2paramId',itemId+"_"+parameterId);
			inputCheck.attr('parentActionItemId',_parentActionItemId);
			inputCheck.attr('type',"checkbox");
			inputCheck.attr('checked',"true");
			inputCheck.attr('grpId',grpId);
			if (rowSpan == 2) {
				inputCheck.attr('style',"float:right;margin-top:5px;display:none");
			} else {
				inputCheck.attr('style',"float:right;margin-top:5px");
			}
			tdActionItem.append(inputCheck);
			
			tr.append(tdParamName);
			tr.append(tdParamValue);
			tr.append(tdActionItem);
			
			//确定是否添加checkbox
			if (checkBoxFlag != itemId) {
				//决定checkbox
				var tdCheckbox  = $("<td>");
				tdCheckbox.attr('align','center');
				tdCheckbox.attr('rowspan',rowSpan);
				
				var inputCheck = $('<input>');
				inputCheck.attr('id','p_paramRelaGrp_Y');
				inputCheck.attr('type',"checkbox");
				inputCheck.attr('checked',"true");
				inputCheck.attr('actionItemId',itemId);
				inputCheck.bind('click',function() {
					var _itemId = $(this).attr('actionItemId');
					if($(this).attr("checked")) {
						 $("input[itemid2paramid^="+_itemId+"_]").attr("checked",'true');//全选
					} else {
						$("input[itemid2paramid^="+_itemId+"_]").removeAttr("checked");//取消全选
					}
				})
		
				tdCheckbox.append(inputCheck);
				tr.append(tdCheckbox);
				
				checkBoxFlag = itemId;
			}
			
			paramTable.append(tr);
		}
	};
	
	
	/**
	 * 根据组ID初始化 checkbox
	 */
	var _initCheckBox = function() {
//		var _select =$("input[itemid2paramid^="+f_getNeedItemId(itemId)+"_]:checked");
//		//能够构成组时
//		var _select =$("input[itemid2paramid^="+f_getNeedItemId(itemId)+"_]");
		
		//根绝决定的主checkbox决定子checkbox
		var isDecideCheckbox = $("input[actionItemId]");
		$.each(isDecideCheckbox, function() {
			var decideCheckbox = $(this);
			var _actionItemId = decideCheckbox.attr('actionItemId');
			
			var _select =$("input[itemid2paramid^="+_actionItemId+"_]");
			//2个决定条目时
			if (_select.length == 2) {
				if ($(_select[0]).attr('grpId') && ($(_select[0]).attr('grpId') == $(_select[1]).attr('grpId'))) {
					decideCheckbox.attr("checked",true);
				} else {
					decideCheckbox.removeAttr('checked');
				}
			}
			//多个决定条目时
			if (_select.length > 2) {
				var _grpIdArr = [];
				var _needCheck;
				//判断
				$.each(_select, function() {
					if (_grpIdArr.contains($(this).attr('grpId'))) {
						_needCheck = $(this).attr('grpId');
					} else {
						_grpIdArr.push($(this).attr('grpId'));
					}
				})
				//操作
				//主
				if (_needCheck) {
					decideCheckbox.attr("checked",true);
				} else {
					decideCheckbox.removeAttr('checked');
				}
				//分支
				$.each(_select, function() {
					if ($(this).attr('grpId') == _needCheck) {
						$(this).attr("checked",true);
					} else {
						$(this).removeAttr('checked');
					}
				})
			}
		})
		
	}
	
	//保存
	var _doSave = function() {
		//不存在直接返回
		var paramActionRelaArray  = tempPack1.paramActionRela;
		if (paramActionRelaArray.length == 0){
			return ;
		}
		
		var isDecide = $("input[actionItemId]:checked");
		var selectRelaActionItemId;
		
		//默认循环赋值
		if (isDecide.length == 0) {
			var flagInt = 1;
			for (var i =0; i< paramActionRelaArray.length; i++) {
				var paramActionRela = paramActionRelaArray[i];
				var parameterId = paramActionRela.getParameterId();
				var itemId = paramActionRela.getActionItemId();
				var parentActionItemId = paramActionRela.getParentActionItemId();
				var value = paramActionRela.getValue();
				
				var _paramActionRela = f_getMemParamActionRela(parentActionItemId,parameterId,itemId);
				_paramActionRela.grpId = flagInt;
				flagInt ++;
			}
			return true;
		}
		
		//转换数据所需的条目与参数
		var actionParameterMap = tempPack1.actionParamterMap;
		
		//已确定决定的条目
		var decideItem = [];
		$.each(isDecide,function() {
			var _itemId = $(this).attr('actionItemId');
			decideItem.push(_itemId);
		})
		
		var flagInt = 1;
		var isDeal = true;
		var dealedItem = [];//被处理过的决定条目
		for (var i =0; i< paramActionRelaArray.length; i++) {
			var paramActionRela = paramActionRelaArray[i];
			var parameterId = paramActionRela.getParameterId();
			var itemId = paramActionRela.getActionItemId();
			var parentActionItemId = paramActionRela.getParentActionItemId();
			var value = paramActionRela.getValue();
			
			//确定是否重复赋值
			var rowSpan = getRowspan(tempPack1,itemId);
			if (rowSpan == 1) {
				var _paramActionRela = f_getMemParamActionRela(parentActionItemId,parameterId, itemId);
				_paramActionRela.grpId = flagInt;
				flagInt ++;
				
				dealedItem.push(itemId);
			}
			
//			itemId = f_getNeedItemId(itemId); 
			
			if (dealedItem.contains(itemId)) {
				continue ;
			}
			if (rowSpan > 1) {
				if (rowSpan == 2) {
					//被决定的组，判断所有成员
					//两个成员时，无选择功能，默认绑定在一起 | 
					if (decideItem.contains(itemId) || decideItem.contains(f_getNeedItemId(itemId))) {
						var _select =$("input[itemid2paramid^="+f_getNeedItemId(itemId)+"_]");
						if (_select.length == 0) {
							_select = $("input[itemid2paramid^="+itemId+"_]");
						}
						var _paramActionRela1 = f_getMemParamActionRela(parentActionItemId,$(_select[0]).attr('paramId'),itemId);
						var _paramActionRela2 = f_getMemParamActionRela((paramActionRelaArray[i+1]).parentActionItemId,$(_select[1]).attr('paramId'),itemId);
						_paramActionRela1.grpId = flagInt;
						_paramActionRela2.grpId = flagInt;
						flagInt ++;
						
						dealedItem.push(itemId);
					} else {
						isDeal = false;
					}
				}
				
				if (rowSpan > 2) {
					//被决定的组，判断所有成员
					if (decideItem.contains(itemId) || decideItem.contains(f_getNeedItemId(itemId))) {
						var _select = $("input[itemid2paramid^="+f_getNeedItemId(itemId)+"_]:checked");
						if (_select.length == 0) {
							_select = $("input[itemid2paramid^="+itemId+"_]:checked");
						}
						//能够构成组时
						var theSameGrpId;
						if(_select.length > 1) {
							var _select =$("input[itemid2paramid^="+f_getNeedItemId(itemId)+"_]");
							if (_select.length == 0) {
								_select = $("input[itemid2paramid^="+itemId+"_]");
							}
							$.each(_select,function() {
								var _paramActionRela = f_getMemParamActionRela($(this).attr('parentActionItemId'),$(this).attr('paramId'),itemId);
								//被多个选中的处理逻辑
								if($(this).attr('checked')) {
									if (dealedItem.contains(itemId) && theSameGrpId) {
										_paramActionRela.grpId = theSameGrpId;
									} else {
										_paramActionRela.grpId = flagInt;
										theSameGrpId = flagInt;
										flagInt ++;
									}
								} else {
									_paramActionRela.grpId = flagInt;
									flagInt ++;
								}
								
								dealedItem.push(itemId);
							});
						} else {
							isDeal = false;
						}
					} else {
						isDeal = false;
					}
				}
				
				//默认处理
				if (!isDeal) {
					var _paramActionRela = f_getMemParamActionRela(parentActionItemId,parameterId,itemId);
					_paramActionRela.grpId = flagInt;
					flagInt ++;
				}
			}
			
			
		}
		return true;
	}
	
	/**
	 * 获得条目
	 * @param {Object} _itemId
	 * @return {TypeName} 
	 */
	var f_getActionItem = function(_itemId) {
		return actionItemMap.get(_itemId) || actionItemMap.get(itemIdMap.getValueFromKey(_itemId));
	}
	
	/**
	 * 修改时，将正规id转换为‘p_’
	 * 
	 */
	var f_getNeedItemId = function(_itemId) {
		if(_itemId && (_itemId.indexOf('p_') == -1)) {
			_itemId = itemIdMap.getValueFromKey(_itemId);
		} 
		
		return _itemId;
	}
	/*
	 * 获得内存中的参数决定条目
	 * 
	 * @param {Object} _parentActionItemId
	 * @param {Object} _parameterId
	 * @param {Object} _itemId
	 * @memberOf {TypeName} 
	 * @return  ParamActionRela 
	 */
	var f_getMemParamActionRela = function(_parentActionItemId, _parameterId, _itemId){
		var _item = f_getActionItem(_parentActionItemId);
		var _actionParamList = _item.getActionItemParameterList();
		if (_actionParamList!= undefined) {
			//确定需操作的对象
			var _actionParam;
			for(var ii = 0;ii < _actionParamList.length;ii++) {
				_actionParam = _actionParamList[ii];
				//同一参数
				if((_actionParam.parameterId == _parameterId)) {
					var _paramActionRelaList = _actionParam.getParameterActionRelaList();
					for (var jj = 0; jj < _paramActionRelaList.length; jj++) {
						tempParamActionRela = _paramActionRelaList[jj];
						//参数决定的条目相同
						if((tempParamActionRela.actionItemId == _itemId) ||
								(tempParamActionRela.actionItemId == f_getNeedItemId(_itemId))) {
							return tempParamActionRela;
						}
					}
				}
			}
		}
	}
	
	var getRowspan = function(_pack,_itemId) {
		var itemNum = 0;
		var paramActionRelaArray  = _pack.paramActionRela;
		$.each(paramActionRelaArray,function() {
			if (this.actionItemId == _itemId) {
				itemNum ++;
			}
		});
		
		return itemNum;
	}
	
	
	/**
	 * 获得条目编号
	 * @param {Object} _actionItemId
	 * @return {TypeName} 
	 */
	var _getItemNumber = function(_actionItemId) {
		var itemNumber;
		//p_条目ID
		if (itemIdMap.getValueFromKey(_actionItemId) != "null") {
			itemNumber = packageContent.getItemNumberByItemId(itemIdMap.getValueFromKey(_actionItemId));
		} else {
		//入库条目ID
			itemNumber = packageContent.getItemNumberByItemId(_actionItemId);
		}
		
		return itemNumber;
	}
	
	
	/**
	 * 业务处理需要重整包
	 */
	var relocatePackForBizProcess = function(packs) {
		var tempPack = {};

		//条目参数构造成Map
		var actionItemMap = packs.getActionItemMap();
		var tempParamActionRelaArray = [];
		var actionParamterMap = new HashMap();
		
		if (actionItemMap!= undefined) {
			var itemValues = actionItemMap.values();
			for(var j = 0;j < actionItemMap.size();j++) {
				var itemValue = itemValues[j];
				var _actionParamList = itemValue.getActionItemParameterList();
				if (_actionParamList!= undefined) {
					//确定需删除对象
					for(var ii = 0;ii < _actionParamList.length;ii++) {
						var _paramActionRelaList = _actionParamList[ii].getParameterActionRelaList();
						actionParamterMap.put(_actionParamList[ii].getParameterId(),_actionParamList[ii]);
						//将参数决定动作信息抽取出来放入数组
						if (_paramActionRelaList) {
							var tempParamActionRela="";
							for (var jj = 0; jj < _paramActionRelaList.length; jj++) {
								tempParamActionRela = _paramActionRelaList[jj];
								tempParamActionRela['parentActionItemId'] = itemValue.actionItemId;
								tempParamActionRela['parameterType'] = _actionParamList[ii].parameterType;
								//加入存在的条目(防止被关联条目的删除)|新增条目，已有条目
								if (actionItemMap.get(tempParamActionRela.actionItemId) ||
										actionItemMap.get(itemIdMap.getValueFromKey(tempParamActionRela.actionItemId))) {
									tempParamActionRelaArray.push(tempParamActionRela);
								}
							}
						}
					}
				}
			}
		}
		
		tempParamActionRelaArray.sort(function(a,b) {
				return a.actionItemId.localeCompare(b.actionItemId);
		})
		tempPack['paramActionRela'] = tempParamActionRelaArray;
		tempPack['actionParamterMap'] = actionParamterMap;
		
		return tempPack;
	}

	//关闭
	var _doClose = function() {
		$("#openActionParameterPage").window('close');
	}

	//初始化包基本信息
	var _init = function() {
		_initParamRelaPageInfo();
		_initCheckBox();
		
		$("#p_paramRelaGrp_save").bind("click", function() {
			var flag = _doSave();
			if (flag) {
				if (flag) {
					_doClose();
				}
			}
		});
		$("#p_paramRelaGrp_cancel").bind("click", function() {
			_doClose();
		});
	}
	_init();
}
